﻿#region 头注释
/******************************************************************************
文件名称    :   HistoryFamily.cs
创建日期    :   2011/5/11 22:11:18
功能描述    :   HistoryFamily实体类
修改日志    :   
    2011-05-11 22:11  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// HistoryFamily实体类
    /// </summary>
    public class HistoryFamily : EntityBase<HistoryFamily, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public HistoryFamily()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            kidneyStone = null;
            hypercalcinuria = null;
            hypercalcinemia = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override HistoryFamily CreateNew()
        {
            return new HistoryFamily();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.HistoryFamily; }
        }

        /// <summary>
        /// 获取或设置肾结石
        /// </summary>
        public bool? KidneyStone
        {
            get { return kidneyStone; }
            set
            {
                if (entityState == EntityState.Saved && kidneyStone != value) entityState = EntityState.Changed;
                kidneyStone = value;
            }
        }
        protected bool? kidneyStone;

        /// <summary>
        /// 获取或设置高钙尿
        /// </summary>
        public bool? Hypercalcinuria
        {
            get { return hypercalcinuria; }
            set
            {
                if (entityState == EntityState.Saved && hypercalcinuria != value) entityState = EntityState.Changed;
                hypercalcinuria = value;
            }
        }
        protected bool? hypercalcinuria;

        /// <summary>
        /// 获取或设置高钙血症
        /// </summary>
        public bool? Hypercalcinemia
        {
            get { return hypercalcinemia; }
            set
            {
                if (entityState == EntityState.Saved && hypercalcinemia != value) entityState = EntityState.Changed;
                hypercalcinemia = value;
            }
        }
        protected bool? hypercalcinemia;

        /// <summary>
        /// 获取或设置Comment
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override HistoryFamily CopyTo(HistoryFamily entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.kidneyStone = this.kidneyStone;
            entity.hypercalcinuria = this.hypercalcinuria;
            entity.hypercalcinemia = this.hypercalcinemia;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<HistoryFamily>
        {
            HistoryFamily entity;
            public DefaultXmlSerializer(HistoryFamily entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public HistoryFamily Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "KidneyStone":
                        entity.kidneyStone = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Hypercalcinuria":
                        entity.hypercalcinuria = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Hypercalcinemia":
                        entity.hypercalcinemia = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.KidneyStone != null) writer.WriteElementString("KidneyStone", XmlConvert.ToString(entity.KidneyStone.Value));
                if (entity.Hypercalcinuria != null) writer.WriteElementString("Hypercalcinuria", XmlConvert.ToString(entity.Hypercalcinuria.Value));
                if (entity.Hypercalcinemia != null) writer.WriteElementString("Hypercalcinemia", XmlConvert.ToString(entity.Hypercalcinemia.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<HistoryFamily>
        {
            HistoryFamily entity;
            public DefaultJsonSerializer(HistoryFamily entity)
                : base()
            {
                this.entity = entity;
            }

            public HistoryFamily Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "kidneyStone":
                        entity.kidneyStone = DbConvert.ToNullableBoolean(propValue) ?? entity.kidneyStone;
                        break;
                    case "hypercalcinuria":
                        entity.hypercalcinuria = DbConvert.ToNullableBoolean(propValue) ?? entity.hypercalcinuria;
                        break;
                    case "hypercalcinemia":
                        entity.hypercalcinemia = DbConvert.ToNullableBoolean(propValue) ?? entity.hypercalcinemia;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //肾结石
                writer.WritePropertyName("kidneyStone");
                if (entity.KidneyStone == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.KidneyStone.Value);
                }

                //高钙尿
                writer.WritePropertyName("hypercalcinuria");
                if (entity.Hypercalcinuria == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Hypercalcinuria.Value);
                }

                //高钙血症
                writer.WritePropertyName("hypercalcinemia");
                if (entity.Hypercalcinemia == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Hypercalcinemia.Value);
                }

                //Comment
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}